const ModelMelodyAuthor = require('../../../models/music/melodyAuthor')
const uuid = require('uuid');
const controlTool = require('../../tool')
const tool = require('../../../utils/commonTools')

// 删除曲作家
exports.delMelodyAuthor = (req, res, next) => {
    ModelMelodyAuthor.delMelodyAuthor(req.query
        , (value) => {
            res.render('success', {
                data: JSON.stringify({
                    message: "删除成功",
                    status: 200,
                    affectedRows: value.affectedRows
                })
            })
        }, (error) => {
            res.render('fail', {
                data: JSON.stringify({
                    message: "删除失败",
                    status: 200,
                    error
                })
            })
        });
}
// 编辑曲作家
exports.updateMelodyAuthor = (req, res, next) => {
    const Data = req.body;
    const { melody_author_id } = Data;
    if (melody_author_id) {
        delete Data.melody_author_id;
        ModelMelodyAuthor.updateMelodyAuthor(Data, {
            melody_author_id
        }, (value) => {
            res.render('success', {
                data: JSON.stringify({
                    message: "更新成功",
                    status: 200,
                    affectedRows: value.affectedRows
                })
            })
        }, (error) => {
            res.render('fail', {
                data: JSON.stringify({
                    message: "更新失败",
                    status: 200,
                    error
                })
            })
        });
    }
}

/** 添加曲作家 */
exports.addMelodyAuthor = (req, res, next) => {
    // const { music_name } = req.body;
    ModelMelodyAuthor.addMelodyAuthor({
        ...req.body,
        melody_author_id: tool.orderCode()
    },
        (value) => {
            res.render('success', {
                data: JSON.stringify({
                    message: "添加成功",
                    status: 200,
                    affectedRows: value.affectedRows
                })
            })
        }, (error) => {
            res.render('fail', {
                data: JSON.stringify({
                    message: "添加失败",
                    status: 200,
                    error
                })
            })
        });
}
/** 查找曲作家 */
exports.findMelodyAuthor = async (req, res, next) => {
    let MelodyAuthor = await ModelMelodyAuthor.findMelodyAuthor(req.body);
    MelodyAuthor.success((value) => {
        if (value.length) {
            res.render('success', {
                data: JSON.stringify({
                    message: "查找到曲作家",
                    value: value,
                    status: 200,
                    length: value.length
                })
            })
        } else {
            res.render('fail', {
                data: JSON.stringify({
                    message: "没有找到曲作家",
                    value: value,
                    length: value.length
                })
            })
        }
    });

    MelodyAuthor.fail((error) => {
        res.render('fail', {
            data: JSON.stringify({
                message: "查找失败",
                error
            })
        })
    })
}
//查找所有曲作家
exports.list = (req, res, next) => {
    const { isAll } = req.body;
    ModelMelodyAuthor.findAll(
        (value) => {
            res.render('success', {
                data: JSON.stringify({
                    message: "查找成功",
                    status: 200,
                    value: value,
                    length: value.length
                })
            })
        }, (error) => {
            res.render('fail', {
                data: JSON.stringify({
                    message: "查找失败",
                    status: 200,
                    error
                })
            })
        }, isAll);
}